package com.sherry.uitils.auth;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class TokenUtil {
    private static final Logger log = LogManager.getLogger(TokenUtil.class);
    public static String getauthkey(Long timestamp){
        String  private_key = "qing_QiNG_jIA_JiAo!#%&@$^*";
        String md5Hex = DigestUtils.md5Hex(private_key + "-" + timestamp);
        return md5Hex;
    }
    public static Long gettimestamp() {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        long time = timestamp.getTime();
        return time;
    }

    public static String getuserToken(Long uid,int usertype) {
        String reString = null;
        Long expireTime = System.currentTimeMillis() + (24*3600*300*1000);
        reString = generatorToken(uid, usertype, expireTime);
        String token = RSAEncrypter.getInstance().encrypt(reString).replace("\r\n", "");
        return token;
    }

    private static String generatorToken(Long id, Integer type, Long expireSeconds){
        String AUTH_SPLIT = "&";
        Long expireTime = System.currentTimeMillis() + (expireSeconds*1000);

        StringBuilder sb = new StringBuilder("v1")
                .append(AUTH_SPLIT).append(id)
                .append(AUTH_SPLIT).append(type)
                .append(AUTH_SPLIT).append(expireTime);

        return sb.toString();
    }

    public static String calRequestMd5(final byte[] data,final String session){

        //calc md5
        final byte[] sessionBytes = session.getBytes(Charset.forName("UTF-8"));

        final ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
        final ByteArrayInputStream sessionStream = new ByteArrayInputStream(sessionBytes);

        InputStream inputStream = new InputStream() {
            private int i = 0;
            public int read () throws IOException {
                int mod = i % 2;
                try {
                    if( data.length < sessionBytes.length ) {
                        int count = data.length * 2;
                        if( mod == 0 && i < count ) {
                            return dataStream.read();
                        } else {
                            return sessionStream.read();
                        }
                    } else {
                        int count = sessionBytes.length * 2;
                        if( mod == 0 && i < count ) {
                            return sessionStream.read();
                        } else {
                            return dataStream.read();
                        }
                    }
                } finally {
                    i++;
                }
            }
        };
        return DigestUtils.md5Hex(String.valueOf(inputStream));
    }

    public static String getSessionId(Long uid,int usertype){
//        String sql = "select * from qq_passport.t_user_session where user_id = " + uid + " and user_type = "+ usertype + " and session_type = 1";//根据用户id获取session
//        String si ="";
//        List<Map<String,Object>> sessionlists = SqlUtil.execSql(sql);
//        if (sessionlists.size() < 1){
//            log.info("uid在t_user_session中不存在");
//        }
//        else{
//            si = sessionlists.get(0).get("session_id").toString();
//        }
//        System.out.println(sql);
//        return si;
        return null;
    }

}
